package cn.huanzi.qch.baseadmin.ecco;

import cn.huanzi.qch.baseadmin.annotation.In;
import cn.huanzi.qch.baseadmin.ecco.home.pojo.Comment;
import cn.huanzi.qch.baseadmin.ecco.home.pojo.Feature;
import cn.huanzi.qch.baseadmin.ecco.home.pojo.Note;
import cn.huanzi.qch.baseadmin.ecco.home.pojo.Release;
import cn.huanzi.qch.baseadmin.ecco.home.service.FeatureService;
import cn.huanzi.qch.baseadmin.ecco.home.service.NoteService;
import cn.huanzi.qch.baseadmin.ecco.json.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/ws/")
public class WSController {

    @Autowired
    private NoteService noteService;

    @Autowired
    private FeatureService featureService;

    @GetMapping(value = "getAssociation")
    public @ResponseBody
    JSONObject getAssociation() {
        JSONObject object=getAssociationFromEcco();
        return transAssociationToGraphWithLinestyle(object);
//        return transAssociationToGraph(object);
    }

    private JSONObject getAssociationFromEcco(){
        JSONObject object=null;
        try {
            HttpClient client = HttpClients.createDefault();
//            if("get".equals(param)){
            HttpGet request = new HttpGet("http://localhost:8080/ecco/artifacts/getAssociation");
            request.setHeader("Accept", "application/json");
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            ObjectMapper mapper = new ObjectMapper();
            object = mapper.readValue(entity.getContent(), JSONObject.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
    @GetMapping(value = "getSingleAssociation")
    public @ResponseBody
    JSONObject getSingleAssociation(String name) {
        String versionname="[d^0("+name+".1)]";
        JSONObject object=getAssociationFromEcco();
        return transSingleAssociationToGraph(object, versionname);
    }
    @GetMapping(value = "getCompareAssociation")
    public @ResponseBody
    JSONObject getCompareAssociation(String name1,String name2) {
        String versionname1="[d^0("+name1+".1)]";
        String versionname2="[d^0("+name2+".1)]";
        JSONObject object=getAssociationFromEcco();
        return transCompareAssociationToGraph(object, versionname1,versionname2);
    }



    @GetMapping(value = "getNote")
    public @ResponseBody
    JSONObject getNote() {
        String rs="{\"code\":0,\"msg\":\"\",\"count\":1,\"data\":[{\"id\":1,\"filename\":\"NumericField.java\",\"line\":72,\"feature\":\"Unicode\",\"sign\":\"添加了对Unicode4的支持\",\"score\":\"\"}]}";
        JSONObject object=JSONObject.parseObject(rs);
        return object;
    }

    @RequestMapping(value = "compose")
    @ResponseBody
    public JSONObject compose(@RequestParam("uuid") String uuid){
        JSONObject object=null;
        try {
            HttpClient client = HttpClients.createDefault();
            HttpPost request = new HttpPost("http://localhost:8080/ecco/artifacts/compose");
            ObjectMapper mapper = new ObjectMapper();
            ArrayList list = new ArrayList();
            list.add(uuid);
            StringEntity requestJson=new StringEntity(mapper.writeValueAsString(list),"utf-8");
            requestJson.setContentType("application/json");
            request.setEntity(requestJson);
            request.setHeader("Accept", "application/json");
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            object = mapper.readValue(entity.getContent(), JSONObject.class);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return object;
        }
    }

    @RequestMapping("/getListShowDetail")
    public JSONObject getListShowDetail(@RequestParam("uuid") String uuid){
        JSONObject object=null;
        try {
            HttpClient client = HttpClients.createDefault();
            HttpPost request = new HttpPost("http://localhost:8080/ecco/artifacts/getMethodList");
            ObjectMapper mapper = new ObjectMapper();
            ArrayList list = new ArrayList();
            list.add(uuid);
            StringEntity requestJson=new StringEntity(mapper.writeValueAsString(list),"utf-8");
            requestJson.setContentType("application/json");
            request.setEntity(requestJson);
            request.setHeader("Accept", "application/json");
            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            object = mapper.readValue(entity.getContent(), JSONObject.class);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return object;
        }
    }

        @RequestMapping(value = "getMigrationNode")
        @ResponseBody
        public JSONObject getMigrationNode(){
            JSONObject object=new JSONObject();
            List<ZNode> list = new ArrayList<>();
            list.add(new ZNode("NumericField",1));
            list.add(new ZNode("SegmentDeletes",2));
            list.add(new ZNode("NormsWriter",3));
            list.add(new ZNode("FreqProxTermsWriter",4));
//            ZNode zNode=new ZNode();
//            zNode.setChilds(list);
            object.put("data",JSONArray.toJSONString(list));
            return object;
        }
    private JSONObject transCompareAssociationToGraph(JSONObject object,String name1,String name2){
        ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) object.get("data");
        ArrayList<NodeJson> nodes=new ArrayList<>();
        ArrayList<LinkJson> links=new ArrayList<>();
        ArrayList<CategoryJson> categories=new ArrayList<>();
        categories.add(new CategoryJson(name1));
        categories.add(new CategoryJson(name2));
        GraphJson json = new GraphJson(nodes, links, categories);

        //第一次遍历，得左右两组件的id，所有组件的大小map，左中右三个部分的组件数
        int id=-1;
        HashSet set = new HashSet();
        ArrayList<Integer> nums=new ArrayList();
        int id1=0;
        int id2=0;
        int countleft=0;
        int countmid=0;
        int countright=0;
        for(HashMap dto:list){
            String condition=(String)dto.get("condition");
            if(condition.indexOf(name1)>-1||condition.indexOf(name2)>-1){
                if(set.add((Integer) dto.get("numArtifacts"))){
                    nums.add((Integer) dto.get("numArtifacts"));
                }
                if(name1.equals(condition)){
                    id++;
                    id1=id;
                }else if(name2.equals(condition)){
                    id++;
                    id2=id;

                }else if(condition.indexOf(name1)>-1&&condition.indexOf(name2)>-1){
                    countmid++;
                }else if(condition.indexOf(name1)>-1){
                    countleft++;
                }else if(condition.indexOf(name2)>-1){
                    countright++;
                }
            }
        }

        //组件大小map
        HashMap numbermap=new HashMap();
        Collections.sort(nums);
        int numberstep=50/nums.size();
        int s=10;
        for(int number:nums){
            s+=numberstep;
            numbermap.put(number,s);
        }

        int leftstep=0;
        int midstep=0;
        int rightstep=0;
        if(countmid>0){
            midstep=1000/countmid;
        }
        if(countleft>0){
            leftstep=1000/countleft;
        }
        if(countright>0){
            rightstep=1000/countright;
        }

        //第二次遍历，创建节点，连接线，分类
        int leftstart=0;
        int rightstart=0;
        int midstart=0;

        for(HashMap dto:list){
            String condition=(String)dto.get("condition");
            if(name1.equals(condition)){
                NodeJson nodeJson=new NodeJson(id1, condition, (Integer) numbermap.get(dto.get("numArtifacts")), -600, 0, ((Integer)dto.get("numArtifacts")),0);
                nodeJson.setAssociationId((String) dto.get("id"));
                nodes.add(nodeJson);
            }else if(name2.equals(condition)){
                NodeJson nodeJson=new NodeJson(id2, condition, (Integer) numbermap.get(dto.get("numArtifacts")), 600, 0, ((Integer)dto.get("numArtifacts")),1);
                nodeJson.setAssociationId((String) dto.get("id"));
                nodes.add(nodeJson);
            }
        }
        for(HashMap dto:list){
            String condition=(String)dto.get("condition");
            if(!name1.equals(condition)&&!name2.equals(condition)){
                if(condition.indexOf(name1)>-1&&condition.indexOf(name2)>-1){
                    midstart=midstart<0?-midstart:-(midstart+midstep);
                    NodeJson nodeJson=new NodeJson(++id, condition, (Integer) numbermap.get(dto.get("numArtifacts")), 0, midstart, ((Integer)dto.get("numArtifacts")),2);
                    nodeJson.setAssociationId((String) dto.get("id"));
                    nodes.add(nodeJson);
                    links.add(new LinkJson(id1, id));
                    links.add(new LinkJson(id2, id));
                }else if(condition.indexOf(name1)>-1){
                    leftstart=leftstart<0?-leftstart:-(leftstart+leftstep);
                    NodeJson nodeJson=new NodeJson(++id, condition, (Integer) numbermap.get(dto.get("numArtifacts")), -1200, leftstart, ((Integer)dto.get("numArtifacts")),3);
                    nodeJson.setAssociationId((String) dto.get("id"));
                    nodes.add(nodeJson);
                    links.add(new LinkJson(id1, id));
                }else if(condition.indexOf(name2)>-1){
                    rightstart=rightstart<0?-rightstart:-(rightstart+rightstep);
                    NodeJson nodeJson=new NodeJson(++id, condition, (Integer) numbermap.get(dto.get("numArtifacts")), 1200, rightstart, ((Integer)dto.get("numArtifacts")),4);
                    nodeJson.setAssociationId((String) dto.get("id"));
                    nodes.add(nodeJson);
                    links.add(new LinkJson(id2, id));
                }
            }
        }
        categories.add(new CategoryJson(name1+"∩"+name2));
        categories.add(new CategoryJson(name1+"∩其他版本"));
        categories.add(new CategoryJson(name2+"∩其他版本"));
        return (JSONObject) JSONObject.toJSON(json);
    }


        //单版本图
        private JSONObject transSingleAssociationToGraph(JSONObject object,String name){
            ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) object.get("data");
            ArrayList<NodeJson> nodes=new ArrayList<>();
            ArrayList<LinkJson> links=new ArrayList<>();
            ArrayList<CategoryJson> categories=new ArrayList<>();
            categories.add(new CategoryJson(name));
            GraphJson json = new GraphJson(nodes, links, categories);


            List<Integer> nums=new ArrayList();
            Set set = new HashSet();
            for(HashMap dto:list){
                String condition= (String) dto.get("condition");
                if(condition.indexOf(name)>-1){
                    if(set.add((int) dto.get("numArtifacts"))){
                        nums.add((int) dto.get("numArtifacts"));
                    }
                }
            }
            //节点显示大小
            Collections.sort(nums);
            HashMap symbosizeMap=new HashMap();
            int s=10;
            int step=50/nums.size();
            for(int a:nums){
                s+=step;
                symbosizeMap.put(a, s);
            }
            //
            int partid=0;
            int xstep=0;
            if(nums.size()>2){
                xstep=2400/nums.size()-2;
            }
            int xstart=0-xstep;
            for(HashMap dto:list){
                String[] condition= ((String) dto.get("condition")).split("OR");
                if(condition.length==1&&condition[0].equals(name)){
                    NodeJson nodeJson=new
                            NodeJson(0, condition[0], Integer.valueOf((Integer) symbosizeMap.get(dto.get("numArtifacts"))),0,-1000,Integer.valueOf((Integer) dto.get("numArtifacts")), 0);
                    nodeJson.setAssociationId((String) dto.get("id"));
                    nodes.add(nodeJson);
                }
            }
            for(HashMap dto:list){
                String[] condition= ((String) dto.get("condition")).split("OR");
                if(condition.length>1&&((String) dto.get("condition")).indexOf(name)>-1){
                    partid++;
                    xstart+=xstep;
                    NodeJson nodeJson=new
                            NodeJson(partid, ((String) dto.get("condition")), Integer.valueOf((Integer) symbosizeMap.get(dto.get("numArtifacts"))),xstart,0,Integer.valueOf((Integer) dto.get("numArtifacts")), partid);
                    nodeJson.setAssociationId((String) dto.get("id"));
                    categories.add(new CategoryJson("part"+partid));//添加category
                    LinkJson linkJson = new LinkJson(0, partid);
                    links.add(linkJson);
                    nodes.add(nodeJson);
                }
            }
            return (JSONObject) JSONObject.toJSON(json);
        }
        private JSONObject transAssociationToGraphWithLinestyle(JSONObject object){
            ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) object.get("data");
            ArrayList<NodeJson> nodes=new ArrayList<>();
            ArrayList<LinkJson> links=new ArrayList<>();
            ArrayList<CategoryJson> categories=new ArrayList<>();
            categories.add(new CategoryJson("part"));
            categories.add(new CategoryJson("Version294"));
            categories.add(new CategoryJson("Version303"));
            categories.add(new CategoryJson("Version310"));
            categories.add(new CategoryJson("Version320"));
            categories.add(new CategoryJson("Version330"));
            categories.add(new CategoryJson("Version340"));
            categories.add(new CategoryJson("Version360"));
            categories.add(new CategoryJson("Version361"));
            categories.add(new CategoryJson("Version362"));
            categories.add(new CategoryJson("Version400"));
            GraphJson json = new GraphJson(nodes, links, categories);
            HashMap dicMap=new HashMap();//独立节点的x轴位置字典表
            List<Integer> nums=new ArrayList();
            Set set = new HashSet();
            List temp=new ArrayList();
            for(HashMap dto:list){
                int num=(int) dto.get("numArtifacts");
                if(set.add(num)){
                    nums.add(num);
                }
            }
            HashMap idMap=new HashMap();
            HashMap<String ,Integer> sortName=new HashMap();
            sortName.put("[d^0(base.1)]",1 );
            sortName.put("[d^0(Version303.1)]",2 );
            sortName.put("[d^0(Version310.1)]",3 );
            sortName.put("[d^0(Version320.1)]",4 );
            sortName.put("[d^0(Version330.1)]",5 );
            sortName.put("[d^0(Version340.1)]",6 );
            sortName.put("[d^0(Version360.1)]",7 );
            sortName.put("[d^0(Version361.1)]",8 );
            sortName.put("[d^0(Version362.1)]",9 );
            sortName.put("[d^0(Version400.1)]",10 );
            temp.add("[d^0(base.1)]");
            temp.add("[d^0(Version303.1)]");
            temp.add("[d^0(Version310.1)]");
            temp.add("[d^0(Version320.1)]");
            temp.add("[d^0(Version330.1)]");
            temp.add("[d^0(Version340.1)]");
            temp.add("[d^0(Version360.1)]");
            temp.add("[d^0(Version361.1)]");
            temp.add("[d^0(Version362.1)]");
            temp.add("[d^0(Version400.1)]");
            HashMap totalMap=new HashMap();
            totalMap.put("[d^0(base.1)]", 0);
            totalMap.put("[d^0(Version303.1)]", 1);
            totalMap.put("[d^0(Version310.1)]", 2);
            totalMap.put("[d^0(Version320.1)]", 3);
            totalMap.put("[d^0(Version330.1)]", 4);
            totalMap.put("[d^0(Version340.1)]", 5);
            totalMap.put("[d^0(Version360.1)]", 6);
            totalMap.put("[d^0(Version361.1)]", 7);
            totalMap.put("[d^0(Version362.1)]", 8);
            totalMap.put("[d^0(Version400.1)]", 9);

            //x位置
            int tempsize=temp.size();
            int xstep=0;
            if(tempsize>1){
                xstep=9600/(tempsize-1);
            }
            int xstart=(-5500)-xstep;
            for (int i = 0; i <tempsize ; i++) {
                xstart+=xstep;
                dicMap.put(temp.get(i),xstart);
            }
            //节点显示大小
            Collections.sort(nums);
            HashMap symbosizeMap=new HashMap();
            int s=0;
//            int step=50/nums.size();
            for(int a:nums){
                s+=1;
                symbosizeMap.put(a, s);
            }
            NodeJson nodeJson1=new NodeJson(0, "base", 50, (int)dicMap.get("[d^0(base.1)]"),-500,100000,1);
            NodeJson nodeJson2=new NodeJson(1, "Version303", 50, (int)dicMap.get("[d^0(Version303.1)]"),-500,100000,2);
            NodeJson nodeJson3=new NodeJson(2, "Version310", 50, (int)dicMap.get("[d^0(Version310.1)]"),-500,100000,3);
            NodeJson nodeJson4=new NodeJson(3, "Version320", 50, (int)dicMap.get("[d^0(Version320.1)]"),-500,100000,4);
            NodeJson nodeJson5=new NodeJson(4, "Version330", 50, (int)dicMap.get("[d^0(Version330.1)]"),-500,100000,5);
            NodeJson nodeJson6=new NodeJson(5, "Version340", 50, (int)dicMap.get("[d^0(Version340.1)]"),-500,100000,6);
            NodeJson nodeJson7=new NodeJson(6, "Version360", 50, (int)dicMap.get("[d^0(Version360.1)]"),-500,100000,7);
            NodeJson nodeJson8=new NodeJson(7, "Version361", 50, (int)dicMap.get("[d^0(Version361.1)]"),-500,100000,8);
            NodeJson nodeJson9=new NodeJson(8, "Version362", 50, (int)dicMap.get("[d^0(Version362.1)]"),-500,100000,9);
            NodeJson nodeJson10=new NodeJson(9, "Version400", 50, (int)dicMap.get("[d^0(Version400.1)]"),-500,100000,10);
            nodes.add(nodeJson1);
            nodes.add(nodeJson2);
            nodes.add(nodeJson3);
            nodes.add(nodeJson4);
            nodes.add(nodeJson5);
            nodes.add(nodeJson6);
            nodes.add(nodeJson7);
            nodes.add(nodeJson8);
            nodes.add(nodeJson9);
            nodes.add(nodeJson10);


            int id=9;
            int ystep=0;
            if(tempsize>1){
                ystep=2400/(tempsize-1);
            }

            for(HashMap dto:list){
                String condition= (String) dto.get("condition");
                NodeJson nodeJson;
                id++;
                int contains=condition.split("OR").length;
                int y=(contains)*ystep;
                nodeJson=new NodeJson(id, condition, Integer.valueOf((Integer) symbosizeMap.get(dto.get("numArtifacts"))),getMid(condition.split("OR"),dicMap),y,Integer.valueOf((Integer) dto.get("numArtifacts")), 0);
                nodeJson.setAssociationId((String) dto.get("id"));
                nodes.add(nodeJson);
                idMap.put(condition, id);
            }
            LinkStyleJson soid=new LinkStyleJson("3", "soild");
            LinkStyleJson soidstrong=new LinkStyleJson("1", "soild");
            LinkStyleJson dotted=new LinkStyleJson("3", "dotted");
            for(HashMap dto:list){
                String condition= (String) dto.get("condition");
                if(condition.split("OR").length>1){
                    int source= (int) idMap.get(condition);
                    String[] targetCondition=condition.split("OR");
                    List<String> targetConditionString=new ArrayList<>();
                    String[] cloneString=new String[10];
                    for(String target:targetCondition){
                        cloneString[sortName.get(target.trim())-1]=target.trim();
                    }
                    for(String one:cloneString ){
                        if(one!=null){
                            targetConditionString.add(one);
                        }
                    }


                    for (int i = 0; i < targetConditionString.size(); i++) {
                        int target= (int) totalMap.get(targetConditionString.get(i).trim());
                        LinkJson linkJson=null;
                        if (i==0){
//                            linkJson = new LinkJson(source, target,soid,new LineLabelJson(true, "出现"));
//                            linkJson = new LinkJson(source, target,soid);
                            linkJson = new LinkJson(source, target,soid,new Emphasis(new LineLabelJson(true, "出现")),new SelectJson(new LineLabelJson(false,"")));
                        }else if(i==targetConditionString.size()-1){
//                            linkJson = new LinkJson(source, target,dotted);
                            linkJson = new LinkJson(source, target,dotted,new Emphasis(new LineLabelJson(true, "消失")),new SelectJson(new LineLabelJson(false,"")));
                        }else {
                            linkJson = new LinkJson(source, target,soidstrong,new Emphasis(new LineLabelJson(true, "存续")),new SelectJson(new LineLabelJson(false,"")));
                        }
                        links.add(linkJson);
                    }
                }else {
                    int source= (int) idMap.get(condition);
                    int target= (int) totalMap.get(condition);
                    LinkJson linkJson = new LinkJson(source, target,soid,new Emphasis(new LineLabelJson(true, "独有")),new SelectJson(new LineLabelJson(false,"")));
                    LinkJson linkJson1 = new LinkJson(source, target,dotted);
                    links.add(linkJson);
                    links.add(linkJson1);
                }
            }
            return (JSONObject) JSONObject.toJSON(json);
        }



        //总览图
        private JSONObject transAssociationToGraph(JSONObject object){
            ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) object.get("data");
            ArrayList<NodeJson> nodes=new ArrayList<>();
            ArrayList<LinkJson> links=new ArrayList<>();
            ArrayList<CategoryJson> categories=new ArrayList<>();
            categories.add(new CategoryJson("part"));
            categories.add(new CategoryJson("Version294"));
            categories.add(new CategoryJson("Version303"));
            categories.add(new CategoryJson("Version310"));
            categories.add(new CategoryJson("Version320"));
            categories.add(new CategoryJson("Version360"));
            GraphJson json = new GraphJson(nodes, links, categories);
            HashMap dicMap=new HashMap();//独立节点的x轴位置字典表
            List<Integer> nums=new ArrayList();
            Set set = new HashSet();
            List temp=new ArrayList();
            for(HashMap dto:list){
                String[] condition= ((String) dto.get("condition")).split("OR");
                int num=(int) dto.get("numArtifacts");
                if(set.add(num)){
                    nums.add(num);
                }
                if(condition.length==1){
                    temp.add(condition[0]);
                }
            }

           //x位置
            int tempsize=temp.size();
            int xstep=0;
            if(tempsize>1){
                xstep=4800/(tempsize-1);
            }
            int xstart=(-2800)-xstep;
            for (int i = 0; i <tempsize ; i++) {
                xstart+=xstep;
                dicMap.put(temp.get(i),xstart);
            }
            //节点显示大小
            Collections.sort(nums);
            HashMap symbosizeMap=new HashMap();
            int s=0;
            int step=50/nums.size();
            for(int a:nums){
                s+=step;
                symbosizeMap.put(a, s);
            }



            int id=-1;
            int ystep=0;
            int categorytype=0;
            if(tempsize>1){
                ystep=1200/(tempsize-1);
            }
            HashMap idMap=new HashMap();
            for(HashMap dto:list){
                String condition= (String) dto.get("condition");
                NodeJson nodeJson;
                id++;
                if(condition.split("OR").length==1){
                    categorytype++;
                    nodeJson=new NodeJson(id, condition, Integer.valueOf((Integer) symbosizeMap.get(dto.get("numArtifacts"))),(int)dicMap.get(condition),-500,Integer.valueOf((Integer) dto.get("numArtifacts")), categorytype);
                    categories.add(new CategoryJson(condition));//添加category
                }else {
                    int contains=condition.split("OR").length;
                    int y=(contains-1)*ystep;
                    nodeJson=new NodeJson(id, condition, Integer.valueOf((Integer) symbosizeMap.get(dto.get("numArtifacts"))),getMid(condition.split("OR"),dicMap),y,Integer.valueOf((Integer) dto.get("numArtifacts")), 0);
                }
                nodeJson.setAssociationId((String) dto.get("id"));
                nodes.add(nodeJson);
                idMap.put(condition, id);
            }
            LinkStyleJson soid=new LinkStyleJson("1", "soild");
            LinkStyleJson soidstrong=new LinkStyleJson("2", "soild");
            LinkStyleJson dotted=new LinkStyleJson("1", "dotted");
            for(HashMap dto:list){
                String condition= (String) dto.get("condition");
                if(condition.split("OR").length>1){
                    int source= (int) idMap.get(condition);
                    String[] targetCondition=condition.split("OR");
                    for(String singelTag:targetCondition){
                        int target= (int) idMap.get(singelTag.trim());
                        LinkJson linkJson = new LinkJson(source, target,dotted);
                        links.add(linkJson);
                    }
                }
            }
            return (JSONObject) JSONObject.toJSON(json);


        }
        public int getMid(String[] names,HashMap map){
            int rs=0;
            for(String name:names){
                rs+=(int)map.get(name.trim());
            }
            return rs/names.length;
        }


        @RequestMapping("/addNote")
        public void addNote(int featureid,String filename,int noteline,String notecontent,String notecode,String noterelease){
            Note note=new Note();
            note.setFeatureid(featureid);
            note.setFilename(filename);
            note.setNoteline(noteline);
            note.setNotecode(notecode);
            note.setNotecontent(notecontent);
            note.setNoterelease(noterelease);
            noteService.add(note);
        }
        @RequestMapping("/getComment")
        public JSONObject getComment(){
            HashMap rs=new HashMap();
            Jedis jedis=new Jedis("localhost",6379);
            List<String> note=jedis.lrange("1_1", 0, -1);
            List<Comment> comments=new ArrayList<>();
            for (String item:note){
                Comment comment=JSONObject.parseObject(item,Comment.class);
                comments.add(comment);
            }
            rs.put("code",0);
            rs.put("msg", "");
            rs.put("count",rs.size());
            rs.put("data", comments);
            JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
            return jsonObject;
        }
        @RequestMapping("/addcomment")
        public void addComment(String name,String comment){
            Jedis jedis=new Jedis("localhost",6379);
            Comment com=new Comment();
            com.setContent(comment);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            com.setTime(formatter.format(date));
            com.setModle("LUCENE-3594");
            com.setFile("FSDirectory.java");
            jedis.lpush(name, JSONObject.toJSONString(com));
            jedis.close();
        }

        @RequestMapping("/findAllByFilename")
        public JSONObject findAllByFilename(HttpServletRequest request){
//            if(filename.isEmpty()) return null;
            String filename=request.getParameter("filename");
            HashMap rs=new HashMap();
            List<Note> notes=noteService.getNoteByFilename(filename);
            rs.put("code",0);
            rs.put("msg", "");
            rs.put("count",rs.size());
            rs.put("data", notes);
            JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
            return jsonObject;
        }
        @RequestMapping("/findAll")
        public JSONObject findAll(HttpServletRequest request){
    //            if(filename.isEmpty()) return null;
            String filename=request.getParameter("filename");
            HashMap rs=new HashMap();
            List<Note> notes=noteService.findAll();
            rs.put("code",0);
            rs.put("msg", "");
            rs.put("count",rs.size());
            rs.put("data", notes);
            JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
            return jsonObject;
        }
        @RequestMapping("/showRelease")
        public JSONObject showRelease(HttpServletRequest request) throws IOException {
            //            if(filename.isEmpty()) return null;


            HashMap rs=new HashMap();
            String dir="D:\\study\\IRTool\\IRTool\\data\\relation.txt";
            BufferedReader in = new BufferedReader(new FileReader(dir));
            String thisline="";
            HashMap<String,List> tmp=new HashMap<>();
            List<Release> releases=new ArrayList<>();
            while ((thisline=in.readLine())!=null){
                String[] mapping=thisline.split("&&");
                if(tmp.get(mapping[1])==null){
                    tmp.put(mapping[1],new ArrayList());
                }else {
                    tmp.get(mapping[1]).add(mapping[2]+"&&"+mapping[3]);
                }
            }
            in.close();
            Iterator<Map.Entry<String, List>> iterator=tmp.entrySet().iterator();
            int id=0;
            Jedis jedis=new Jedis("localhost",6379);
            String releasedir="D:\\study\\IRTool\\IRTool\\data\\lucene-release\\version3.6.0\\";
            while (iterator.hasNext()){
                Map.Entry<String, List> entry=iterator.next();
                Release release=new Release();
                release.setId(id);
                id++;
                release.setRelease(entry.getKey());
                String line;
                StringBuffer content=new StringBuffer();
                try {
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(releasedir+entry.getKey()+".txt"));
                    while((line = bufferedReader.readLine()) != null)
                        content.append(line);// 每次读取一行，直到文件结束
                    bufferedReader.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                release.setContent(content.toString());

                HashMap<String,ArrayList> map=new HashMap<>();
                map.put("current", new ArrayList());
                map.put("past", new ArrayList());
                jedis.del("360:"+entry.getKey()+":current");
                jedis.del("360:"+entry.getKey()+":past");
                for(Object javafile:entry.getValue()){
                    String version=javafile.toString().split("&&")[0];
                    if(version.equals("d^0(Version360)")){
                        map.get("current").add(javafile);
                        jedis.lpush("360:"+entry.getKey()+":current", (String) javafile);
                    }else {
                        map.get("past").add(javafile);
                        jedis.lpush("360:"+entry.getKey()+":past", (String) javafile);
                    }
                }
                release.setJavalist(map);
                releases.add(release);
            }
            jedis.close();
            rs.put("code",0);
            rs.put("msg", "");
            rs.put("count",rs.size());
            rs.put("data", releases);
            JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
            return jsonObject;
        }

    @RequestMapping("/getXiala")
    public JSONObject getXiala(HttpServletRequest request){
        //            if(filename.isEmpty()) return null;
        HashMap rs=new HashMap();
        JSONObject object=getAssociationFromEcco();
        ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) object.get("data");
        ArrayList optionlist=new ArrayList();
        for (HashMap dto:list){
            String[] conditon=((String)dto.get("condition")).split("OR");
            if(conditon.length==1){
                String name=conditon[0].substring(conditon[0].indexOf("(")+1,conditon[0].indexOf("."));
                optionlist.add(name);
            }
        }
        rs.put("code",0);
        rs.put("msg", "");
        rs.put("count",rs.size());
        rs.put("data", optionlist);
        JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
        return jsonObject;
    }


    @RequestMapping("/findNum")
    public JSONObject findNum(HttpServletRequest request){
        //            if(filename.isEmpty()) return null;
        HashMap rs=new HashMap();
        List nums=noteService.findNum();
        rs.put("code",0);
        rs.put("msg", "");
        rs.put("count",rs.size());
        rs.put("data", nums);
        JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
        return jsonObject;
    }
    @RequestMapping("/findFeatureAll")
    public JSONObject findFeatureAll(HttpServletRequest request){
        HashMap rs=new HashMap();
        List<Feature> features=featureService.findAll();
        rs.put("code",0);
        rs.put("msg", "");
        rs.put("count",rs.size());
        rs.put("data", features);
        JSONObject jsonObject= (JSONObject) JSONObject.toJSON(rs);
        return jsonObject;
    }
}
